<!DOCTYPE html>
<html>
	<head>
		<title>历史记录</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
		<link rel="stylesheet" href="./css/bootstrap.min.css">
	</head>
	<body>
		<script src="./js/hybrid_html_uni.webview.1.5.5.js"></script>
		<script src="./js/common.js"></script>
		<h2 id="notice">文件预计24小时后删除，请及时保存</h1>
			<div style="padding: 0.5vw 3vw;">
				<button class="btn btn-sm btn-default " onclick="uniPage()">菜单</button>
				<button class="btn btn-danger btn-sm pull-right" onclick="delHistory()">清空</button>
			</div>
			<h3 id="tips">
		</h2>
		<table class="table table-striped" width="100%">
			<thead>
				<tr>
					<td style="max-width:70%">名称/更新时间</td>
					<td width="60">大小</td>
					<td width="60">操作</td>
				</tr>
			</thead>
			<tbody id="tbody">

			</tbody>
		</table>

		<div style="position: fixed;left: 45%;bottom: 10%;border:1px solid #999;border-radius: 5px ;">
			<img src="img/refresh .png" width="28" height="28" onclick="window.location.reload()" />
		</div>
		<script>
			var inited = false;
			
			function init() {
				if (inited) {
					return;
				}
				inited = true;
				var v = GetParam("v");
				if (v && v < 1.2) {
					document.getElementById("notice").innerHTML = "有新版本,可前往更新<br/><a href='javascript:void(0)' onclick=\"openUrl('http://spider.sumroads.site')\" target='_blank' style='font-size:18px'>http://spider.sumroads.site</a>";
				} else {
					document.getElementById("notice").innerHTML += 'v' + v;
				}
				
				var historyData = getHistory();
				if (historyData.length == 0) {
					return;
				}
				if (document.getElementById("tips")) {
					document.getElementById("tips").innerHTML = "加载中";
				}
				let names = historyData.map(item => item.name);
				let name = names.join(';');
				var data = {
					"name": name,
				};
				console.log(name)
				
				var xhr = new XMLHttpRequest();
				xhr.open("POST", "/api/bookList", true);
				xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xhr.onreadystatechange = function() {
					if (xhr.readyState === 4 && xhr.status === 200) {
						if (document.getElementById("tips")) {
							document.getElementById("tips").innerHTML = "";
						}
						console.log(xhr.responseText)
						var res = JSON.parse(xhr.responseText)
						let tpl =
							`<tr><td>{name}<br><span class="text-muted">{time}</span></td><td>{size}k</td><td><a href="{url}" class="btn btn-link" download="{name}.txt" target="_blank" onclick="unidown('{url}','{name}.txt')" >保存</a></td></tr>`;
						if (v != "") {
							//alert(tpl)
							tpl = tpl.replace(/<a/g, '<span');
							tpl = tpl.replace(/<\/a/g, '</span');
						}
						var tbody = document.getElementById('tbody')
						if (res.Code == 0) {
							for (var book of names) {
								let tr = tpl.replace(/{name}/g, book)
								if (!res.Data[book]) {
									tr = tr.replace('{time}', '')
									tr = tr.replace(/{url}/g, '')
									tr = tr.replace('{size}', 0)
									tr = tr.replace('保存', '')
									tbody.insertAdjacentHTML('beforeend', tr)
									continue
								}
								if (res.Data[book]['url']) {
									tr = tr.replace('{time}', res.Data[book]['time'])
									tr = tr.replace(/{url}/g, res.Data[book]['url'])
									tr = tr.replace('{size}', res.Data[book]['size'])
								} else {
									tr = tr.replace('{time}', '')
									tr = tr.replace(/{url}/g, '')
									tr = tr.replace('{size}', 0)
									tr = tr.replace('保存', '')
								}


								tbody.insertAdjacentHTML('beforeend', tr)
							}
						} else {
							alert("Error: " + xhr.responseText);
						}

					} else if (xhr.readyState === 4) {

						alert("Error: " + xhr.responseText);
					}
				};
				xhr.send(objectToFormUrlEncoded(data));
			}
			init();
			document.addEventListener('DOMContentLoaded', init);
			
			function openUrl(url){
				plus.runtime.openURL( url,(err)=>{
					console.log(err)
				})
			}
		</script>
	</body>
</html>